package cn.androidstudy.course04;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    //响应按钮单击事件，用来打开对话框
    public void openDialog(View view){

        //使用一个方法打开对话框，使用id区分对话框类型
        switch (view.getId()){
            case R.id.btn_dialog:
                AlertDialog dialog;
                dialog = new AlertDialog.Builder(this)
                        .setTitle("Dialog对话框")
                        .setMessage("是否确定退出？")
                        .setIcon(R.mipmap.ic_launcher)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                finish();
                            }
                        })
                        .setNegativeButton("取消", null)
                        .create();
                dialog.show();

                break;
            case R.id.btn_radio_dialog:
                final boolean[] checkedItems = { true, false };// 存放选中状态，true为选中
                // ，false为未选中，和setSingleChoiceItems中第二个参数对应
                new AlertDialog.Builder(this)
                        .setTitle("请选择性别")
                        .setIcon(R.mipmap.ic_launcher)
                        // 第一个参数存放选项的数组，第二个参数存放默认被选中的项，第三个参数点击事件
                        .setSingleChoiceItems(new String[]{"男", "女"}, 0,
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        for (int i = 0; i < checkedItems.length; i++) {
                                            checkedItems[i] = false;
                                        }
                                        checkedItems[which] = true;
                                    }
                                })
                        .setPositiveButton("确定", null)
                        .show();

                break;
            case R.id.btn_check_dialog:
                final String[] items = { "旅游", "美食", "汽车", "宠物"};// 存放选项的数组
                final boolean[] checkedItems2 = { false, true, false, false };
                AlertDialog 确定 = new AlertDialog.Builder(this)
                        .setTitle("请添加兴趣爱好！")
                        .setIcon(R.mipmap.ic_launcher)
                        .setMultiChoiceItems(items,
                                checkedItems2,
                                new DialogInterface.OnMultiChoiceClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                        if (isChecked) {
                                            checkedItems2[which] = true;
                                        } else {
                                            checkedItems2[which] = false;
                                            myToast("您取消了" + items[which]);
                                        }
                                    }
                                })
                        .setPositiveButton("确定", null)
                        .show();
                break;
            case R.id.btn_list_dialog:
                final String[] listItems = { "苹果", "草莓", "橘子", "冬瓜" };
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("我是列表对话框");
                builder.setItems(listItems, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        myToast(listItems[which]);
                    }
                });
                builder.create().show();
            case R.id.btn_progress_dialog:
                ProgressDialog dialog1 = new ProgressDialog(MainActivity.this);
                dialog1.setTitle("不带进度条的对话框");
                dialog1.setMessage("正在加载中~");
                dialog1.show();
                break;

            case R.id.btn_progress_dialog2:
                final ProgressDialog dialog2 = new ProgressDialog(MainActivity.this);
                dialog2.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 设置水平样式
                dialog2.setTitle("带进度条的对话框");
                dialog2.setMessage("加载中~");
                dialog2.setMax(200);
                dialog2.show();
                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        for (int j = 0; j <= 200; j++) {
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            dialog2.setProgress(j);
                        }
                    }
                }).start();
                break;
            case R.id.btn_shape:
                Intent intent=new Intent(MainActivity.this,ShapeActivity.class);
                startActivity(intent);
                break;
        }

    }

    private void myToast(String msg){
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        Log.d(TAG, "myToast: ");

    }
}
